from server.app.cloud_ide.schema.ide_environment.request.ide_environment_create_request import \
    IdeEnvironmentCreateRequest
from server.app.cloud_ide.schema.ide_environment.request.ide_environment_update_request import \
    IdeEnvironmentUpdateRequest
from server.app.cloud_ide.schema.ide_registry.request.ide_registry_create_request import IdeRegistryCreateRequest
from server.app.cloud_ide.schema.ide_registry.request.ide_registry_update_request import IdeRegistryUpdateRequest
from server.app.cloud_ide.service.ide_environment_service import IdeEnvironmentService
from server.app.cloud_ide.service.ide_registry_service import IdeRegistryService
from behave import *

from server.framework.core.database import SessionLocal
from server.framework.core.logger import logger

ide_environment_service = IdeEnvironmentService()
db = SessionLocal()


@step('查看资源配额详情列表')
def 查看资源配额详情列表(context):
    ide_environment_service.get_ide_environment_list(db=db, page_size=10, current_page=1,
                                                     name=None)


@step('新增资源配额,信息如下')
def 新增资源配额_信息如下(context):
    for row in context.table:
        rs = ide_environment_service.create_ide_environment(db=db, data=IdeEnvironmentCreateRequest(
            name=row['name'],
            max_cpu=row['max_cpu'],
            max_memory=row['max_memory'],
            max_disk=row['max_disk'],
            status=row['status'],
        ))
        assert rs


@step('查看资源配额[{name}]的详情信息')
def 查看资源配额_的详情信息(context, name):
    target = ide_environment_service.find_ide_environment_by_name(db=db, name=name)
    rs = ide_environment_service.get_ide_environment_by_id(db, target.id)
    assert rs is not None


@step('删除以下资源配额')
def 删除以下资源配额(context):
    names = [x['name'] for x in context.table]
    for name in names:
        target = ide_environment_service.find_ide_environment_by_name(db=db, name=name)
        if target is not None:
            ide_environment_service.delete_ide_environment(db=db, ids=[str(target.id)])


@step('编辑资源配额[{name}]的详情信息,将资源配额信息修改为')
def 编辑资源配额_的详情信息_将资源配额信息修改为(context, name):
    target = ide_environment_service.find_ide_environment_by_name(db=db, name=name)
    for row in context.table:
        rs = ide_environment_service.update_ide_environment(db=db, data=IdeEnvironmentUpdateRequest(
            name=row['name'],
            max_cpu=row['max_cpu'],
            max_memory=row['max_memory'],
            max_disk=row['max_disk'],
            status=row['status'],
            id=str(target.id)
        ))
        assert rs
